﻿package morn.core.managers
{
    import __AS3__.vec.*;
    import com.sh.game.pool.*;
    import flash.utils.*;
    import morn.editor.core.*;

    public class SkinManager extends Object
    {
        private var _list:Vector.<SkinProcess>;
        private var _allViewSkinProcess:Dictionary;
        private var _allViewInitHandler:Dictionary;
        private var _updateCount:int = 0;
        private var _enabled:Boolean = false;
        private var _has:Dictionary;
        public static const SkinProcessClass:String = "morn.core.managers.SkinProcess";

        public function SkinManager()
        {
            this._list = new Vector.<SkinProcess>;
            this._allViewSkinProcess = new Dictionary();
            this._allViewInitHandler = new Dictionary();
            this._has = new Dictionary();
            ObjectPoolManager.getInstance().initPool(SkinProcessClass, 500);
            return;
        }// end function

        public function skinLater(param1:ISkinAbleContainer, param2:Object, param3:String) : void
        {
            var _loc_4:* = ObjectPoolManager.getInstance().borrowItem(SkinProcessClass) as SkinProcess;
            (ObjectPoolManager.getInstance().borrowItem(SkinProcessClass) as SkinProcess).setData(param1, param2);
            if (!this._allViewSkinProcess[param3])
            {
                this._allViewSkinProcess[param3] = new Vector.<SkinProcess>;
            }
            this._allViewSkinProcess[param3].push(_loc_4);
            return;
        }// end function

        public function addToProcess(param1:String) : void
        {
            var _loc_4:SkinProcess = null;
            if (this._has[param1])
            {
                return;
            }
            var _loc_2:* = this._allViewSkinProcess[param1];
            if (_loc_2 != null && _loc_2.length > 0)
            {
                if (this._list.length > 0)
                {
                    this._list = this._list.concat(_loc_2);
                    delete this._allViewSkinProcess[param1];
                }
                else
                {
                    this._list = _loc_2;
                }
                if (this._list.length > 0 && this._enabled == false)
                {
                    this._enabled = true;
                    App.timer.doFrameLoop(1, this.update, null);
                }
            }
            var _loc_3:* = this._allViewSkinProcess[param1 + "list"];
            if (_loc_3 && _loc_3.length > 0)
            {
                while (_loc_3.length > 0)
                {
                    
                    _loc_4 = _loc_3.shift();
                    _loc_4.process();
                    ObjectPoolManager.getInstance().returnItem(SkinProcessClass, _loc_4);
                }
                delete this._allViewSkinProcess[param1 + "list"];
            }
            this._has[param1] = true;
            return;
        }// end function

        public function addViewInitHandler(param1:String, param2:Function) : void
        {
            if (!this._allViewInitHandler[param1])
            {
                this._allViewInitHandler[param1] = new Vector.<Function>;
            }
            this._allViewInitHandler[param1].unshift(param2);
            return;
        }// end function

        public function initHandler(param1:String) : void
        {
            var _loc_2:Vector.<Function> = null;
            var _loc_3:Function = null;
            if (this._allViewInitHandler[param1] != null)
            {
                _loc_2 = this._allViewInitHandler[param1];
                for each (_loc_3 in _loc_2)
                {
                    
                    if (_loc_3 != null)
                    {
                        this._loc_3();
                    }
                }
                this._allViewInitHandler[param1] = null;
                delete this._allViewInitHandler[param1];
            }
            return;
        }// end function

        public function update() : void
        {
            var _loc_1:SkinProcess = null;
            this._updateCount = 0;
            while (this._updateCount < 20 && this._list.length > 0)
            {
                
                _loc_1 = this._list.shift();
                _loc_1.process();
                ObjectPoolManager.getInstance().returnItem(SkinProcessClass, _loc_1);
                var _loc_2:String = this;
                var _loc_3:* = this._updateCount + 1;
                _loc_2._updateCount = _loc_3;
            }
            if (this._list.length == 0)
            {
                this._enabled = false;
                App.timer.clearTimer(this.update);
            }
            return;
        }// end function

    }
}
